package com.ubxtech.mqtt.handler;

import lombok.extern.slf4j.Slf4j;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHandler;
import org.springframework.messaging.MessageHeaders;
import org.springframework.messaging.MessagingException;

/**
 *
 * @author Victor.Xiao
 * @since 2022-03-29 16:14
*/
@Slf4j
public abstract class AbstractReceiveMessageHandler implements MessageHandler {
    @Override
    public void handleMessage(Message<?> message) throws MessagingException {
        log.info("收到mqtt消息:{}", message);
        String payload = String.valueOf(message.getPayload());
        MessageHeaders headers = message.getHeaders();
        String topic = String.valueOf(headers.get("mqtt_receivedTopic"));
        receiveMessage(payload,topic);
    }

    /**
     * 接受数据
     * @author Victor.Xiao
     * @since 2022-03-30 10:32
     * @param payload payload
     * @param topic topic
    */
    public abstract void receiveMessage(String payload, String topic);
}
